﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SQLite;
using System.Windows.Controls;

namespace MOJsdk
{
    public class FileManageTools
    {
        public static void AddToDelete(string FilePath)
        {
            if (!File.Exists(FilePath))
                return;

            using (SQLiteConnection _cn = new SQLiteConnection(Path.Combine(MojHost.GetHost.MoJSystem.ApplicationFolder, "delete.db")))
            {
                _cn.CreateTable<FileToDelete>();

                if (_cn.Table<FileToDelete>().Count(z => z.FilePath.ToLower() == FilePath.ToLower()) == 0)
                    _cn.Insert(new FileToDelete() { FilePath = FilePath });
            }
        }
        public static void FlushFiles()
        {
            using (SQLiteConnection _cn = new SQLiteConnection(Path.Combine(MojHost.GetHost.MoJSystem.ApplicationFolder, "delete.db")))
            {
                _cn.CreateTable<FileToDelete>();
                List<FileToDelete> files = new List<FileToDelete>(_cn.Table<FileToDelete>());
                foreach (var f in files)
                {
                    try
                    {
                        File.Delete(f.FilePath);
                    }
                    catch { }

                    if (!File.Exists(f.FilePath))
                        _cn.Delete(f);
                }
            }
        }

        private class FileToDelete
        {
            [PrimaryKey]
            public string FilePath
            { get; set; }
        }
    }

    public class FilePoolInterop
    {
        private MojDynamicVars _files;

        public FilePoolInterop()
        {
            _files = new MojDynamicVars();
        }

        public void AddFile(string FilePath)
        {
            string f = FilePath.ToLower();

            if (!File.Exists(f)) return;
            if (_files.Exist(f)) return;

            _files.Set(f, f);
        }
        public void RemoveFile(string FilePath)
        {
            string f = FilePath.ToLower();
            _files.Remove(f);
        }
        public List<string> LoadFiles()
        {
            return _files.GetKeys();
        }
    }

    public class StaticFileCollection
    {
        private List<string> _files;

        public StaticFileCollection()
        { 
            _files = new List<string>();
            OnSizeChange += delegate { };
            OnFileSelected += delegate { };
        }

        public bool AddFile(string FilePath)
        {
            if (!File.Exists(FilePath) && !_files.Contains(FilePath) &&
                MojHost.GetHost.MoJSystem.FilesInterop.PathIndex(FilePath) != -1)
                return false;

            _files.Add(FilePath);
            OnSizeChange(this);
            return true;
        }
        public bool RemoveFile(string FilePath)
        {
            if (!_files.Contains(FilePath))
                return false;

            _files.Remove(FilePath);
            OnSizeChange(this);
            return true;
        }
        public void CheckFiles()
        {
            List<string> temp = new List<string>();
            foreach (var v in _files)
                if (MojHost.GetHost.MoJSystem.FilesInterop.PathIndex(v) != -1)
                    temp.Add(v);

            foreach (var v in temp)
                _files.Remove(v);

            if (temp.Count > 0)
                OnSizeChange(this);
        }
        public List<string> FileCollection
        { get { return _files; } }
        public MenuControl BuildControl(int Width, int Height)
        {
            Button styleBtn = new Button();
            styleBtn.ApplyStyle("MOJ_MenuControl_Button");

            MenuControl _menu = new MenuControl();
            _menu.ButtonHeight = Height;
            _menu.ButtonWidth = Width;
            _menu.ButtonStyle = styleBtn.Style;

            CheckFiles();

            foreach (var v in _files)
            {
                Button btn = new Button();
                btn.Content = Path.GetFileNameWithoutExtension(v);
                btn.ToolTip = v;
                btn.Click += delegate { OnFileSelected(this, v); };
                _menu.AddNewButton(btn);
            }

            Button open = new Button();
            open.Content = MOJLocalization.Localize("Browse File");
            open.Click += delegate { OnFileSelected(this, "OPEN"); };
            _menu.AddNewButton(open);

            return _menu;
        }

        public event CallEventHandler OnSizeChange;
        public event StringSelectionHandler OnFileSelected;
    }
}
